//
//  WSDataReportFloatingListViewController.m
//  microvision
//
//  Created by zhenjingxiong on 2021/3/2.
//  Copyright © 2021 tencent. All rights reserved.
//

#import "WSDataReportFloatingListViewController.h"
#import "WSDataReportFilterManager.h"
#import "WSDataReportFullScreenViewController.h"
#import "WSUpdateFilterConditionViewController.h"
#import "WSDataReportConfig.h"
#import "WSDataReportFullScreenCell.h"
#import "WSDataReportDetailViewController.h"
#import "WSDataReportHelper.h"
#import "WSDataReportDataSourceManager.h"
#import "WSShowFilterConditionView.h"
#import "WSDataReportToastView.h"

@interface WSDataReportFloatingListViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
WSDataReportFullScreenDelegate,
WSDataReportManagerDelegate,
WSDataReportDetailGoBackDelegate
>

@property (nonatomic, strong) UIButton *closePageButton;
@property (nonatomic, strong) UIButton *showFullScreenButton;
@property (nonatomic, strong) UIButton *updateFilterConditionButton;
@property (nonatomic, strong) UIButton *clearFilterConditionButton;
@property (nonatomic, strong) UIButton *clearingCurrentDataButton;
@property (nonatomic, strong) UITableView *floatingListView;
@property (nonatomic, strong) WSShowFilterConditionView *showFilterConditionView;
@property (nonatomic, strong) WSDataReportFilterManager *filterManager;

@end

@implementation WSDataReportFloatingListViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.view.hidden = NO;
    CGFloat kScreenHeight = UIScreen.mainScreen.bounds.size.height;
    CGFloat kScreenWidth = UIScreen.mainScreen.bounds.size.width;
    self.view.frame = CGRectMake(10, (kScreenHeight - 250) / 2, kScreenWidth - 20, 300);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self.floatingListView  registerClass:[WSDataReportFullScreenCell class]
                   forCellReuseIdentifier:NSStringFromClass([WSDataReportFullScreenCell class])];
    [[WSDataReportFilterManager instance] registerObserver:self];
    [self setNeedsStatusBarAppearanceUpdate];
}

#pragma mark - setupUI Method

- (void)setupUI {
    [self.view addSubview:self.closePageButton];
    self.closePageButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self closePageButtonConstraints]];
    
    [self.view addSubview:self.showFullScreenButton];
    self.showFullScreenButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self showFullScreenButtonConstraints]];

    [self.view addSubview:self.clearingCurrentDataButton];
    self.clearingCurrentDataButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self clearingCurrentDataButtonConstraints]];
    
    [self.view addSubview:self.updateFilterConditionButton];
    self.updateFilterConditionButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self updateFilterConditionButtonConstraints]];
    
    [self.view addSubview:self.clearFilterConditionButton];
    self.clearFilterConditionButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self uclearFilterConditionButtonConstraints]];
    
    [self.view addSubview:self.showFilterConditionView];
    self.showFilterConditionView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self showFilterConditionViewConstraints]];
    
    [self.view addSubview:self.floatingListView];
    self.floatingListView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self floatingListViewLeadingConstraints]];
}


#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSDataReportFullScreenCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSDataReportFullScreenCell class]) forIndexPath:indexPath];
    [cell setShowLabelData:[WSDataReportFilterManager instance].filterDataSource[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [WSDataReportFilterManager instance].filterDataSource.count;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *info = [WSDataReportFilterManager instance].filterDataSource[indexPath.row];
    WSDataReportDetailViewController *detailViewController = [[WSDataReportDetailViewController alloc] init];
    detailViewController.reportDataDictionary = info;
    detailViewController.delegate = self;
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navigationController animated:YES completion:nil];
}

#pragma mark - WSDataReportManagerDelegate

- (void)updatedFilterDataSource {
    [self.floatingListView reloadData];
}

- (void)updateFilterConditionViewContent:(NSString *)filterContent {
    [self.showFilterConditionView updateFilterConditionViewContent:filterContent];
}

#pragma mark - WSDataReportDetailGoBackDelegate
- (void)goBackFromDetailViewController {
    [self dismissViewControllerAnimated:YES completion:nil];
    self.view.hidden = NO;
}

#pragma mark - WSDataReportFullScreenDelegate

- (void)closeFullScreenPage {
    self.view.hidden = NO;
}

- (void)confirmFilterCondition {
    [self dismissViewControllerAnimated:YES completion:nil];
    self.view.hidden = NO;
}

- (void)goBackPreviousPage {
    [self dismissViewControllerAnimated:YES completion:nil];
    self.view.hidden = NO;
}

#pragma mark - UIResponder touch Method

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    // 获取上一个点
    CGPoint prePoint = [touch previousLocationInView:self.view];
    CGFloat offsetX = currentPoint.x - prePoint.x;
    CGFloat offsetY = currentPoint.y - prePoint.y;
    self.view.transform = CGAffineTransformTranslate(self.view.transform, offsetX, offsetY);
}

#pragma mark - Private Method

- (void)showFullScreenDataReport {
    WSDataReportFullScreenViewController *vc = [[WSDataReportFullScreenViewController alloc] init];
    vc.delegete = self;
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navigationController animated:YES completion:nil];
    
}

- (void)goToUpdateFilterConditionPage {
    WSUpdateFilterConditionViewController *vc = [[WSUpdateFilterConditionViewController alloc] init];
    vc.delegete = self;
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navigationController animated:YES completion:nil];
}

- (void)closePageView {
   if ([self.delegate respondsToSelector:@selector(closeReportFloatingPageView)]) {
       [self.delegate closeReportFloatingPageView];
   }
}

- (void)clearCurrentReportData {
    [[WSDataReportDataSourceManager instance] removeCurrentAllReportData];
    [WSDataReportToastView showWithStatus:@"清除数据成功"];
}

- (void)clearFilterCondition {
    [[WSDataReportFilterManager instance] clearFilterFilterConditions];
    [WSDataReportToastView showWithStatus:@"清除筛选条件成功"];
}

#pragma mark - Accessor Method

- (UIButton *)closePageButton {
    if (!_closePageButton) {
        _closePageButton = [UIButton new];
        UIImage *image = [[UIImage imageNamed:@"icon_hepai_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [_closePageButton.imageView setTintColor:[UIColor whiteColor]];
        [_closePageButton setImage:image forState:UIControlStateNormal];
        [_closePageButton addTarget:self action:@selector(closePageView)
                   forControlEvents:UIControlEventTouchUpInside];
    }
    return _closePageButton;
}

- (UIButton *)showFullScreenButton {
    if (!_showFullScreenButton) {
        _showFullScreenButton = [UIButton new];
        _showFullScreenButton.backgroundColor = [UIColor redColor];
        _showFullScreenButton.titleLabel.font = [UIFont systemFontOfSize:10];
        [_showFullScreenButton setTitle:@"点击展开全部" forState:UIControlStateNormal];
        [_showFullScreenButton addTarget:self action:@selector(showFullScreenDataReport)
                        forControlEvents:UIControlEventTouchUpInside];
    }
    return _showFullScreenButton;
}

- (UIButton *)updateFilterConditionButton {
    if (!_updateFilterConditionButton) {
        _updateFilterConditionButton = [UIButton new];
        _updateFilterConditionButton.backgroundColor = [UIColor redColor];
        _updateFilterConditionButton.titleLabel.font = [UIFont systemFontOfSize:10];
        [_updateFilterConditionButton setTitle:@"更新筛选条件" forState:UIControlStateNormal];
        [_updateFilterConditionButton addTarget:self action:@selector(goToUpdateFilterConditionPage)
                               forControlEvents:UIControlEventTouchUpInside];
    }
    return _updateFilterConditionButton;
}

- (UIButton *)clearingCurrentDataButton {
    if (!_clearingCurrentDataButton) {
        _clearingCurrentDataButton = [UIButton new];
        _clearingCurrentDataButton.backgroundColor = [UIColor redColor];
        _clearingCurrentDataButton.titleLabel.font = [UIFont systemFontOfSize:10];
        [_clearingCurrentDataButton setTitle:@"清空当前数据" forState:UIControlStateNormal];
        [_clearingCurrentDataButton addTarget:self action:@selector(clearCurrentReportData)
                              forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearingCurrentDataButton;
}

- (UITableView *)floatingListView {
    if (!_floatingListView) {
        _floatingListView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _floatingListView.delegate = self;
        _floatingListView.dataSource = self;
        _floatingListView.tableFooterView = [UIView new];
        _floatingListView.backgroundColor = [UIColor clearColor];
        _floatingListView.separatorStyle = UITableViewCellSeparatorStyleNone;
        NSArray *array = [WSDataReportConfig instance].listViewShowKeyArray;
        _floatingListView.rowHeight = 20 * array.count + 5;
    }
    return _floatingListView;
}

- (UIButton *)clearFilterConditionButton {
    if (!_clearFilterConditionButton) {
        _clearFilterConditionButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        _clearFilterConditionButton.backgroundColor = [UIColor redColor];
        _clearFilterConditionButton.titleLabel.font = [UIFont systemFontOfSize:10];
        [_clearFilterConditionButton setTitle:@"清除筛选条件" forState:UIControlStateNormal];
        [_clearFilterConditionButton addTarget:self action:@selector(clearFilterCondition)
                              forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearFilterConditionButton;
}

- (WSShowFilterConditionView *)showFilterConditionView {
    if (!_showFilterConditionView) {
        _showFilterConditionView = [WSShowFilterConditionView new];
        _showFilterConditionView.backgroundColor = [UIColor redColor];
    }
    return _showFilterConditionView;
}

#pragma mark - Automatic layout Method

- (NSArray<NSLayoutConstraint *> *)closePageButtonConstraints {
    NSLayoutConstraint *closePageButtonHeight = [NSLayoutConstraint constraintWithItem:self.closePageButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:30];
    NSLayoutConstraint *closePageButtonLeading = [NSLayoutConstraint constraintWithItem:self.closePageButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:7];
    NSLayoutConstraint *closePageButtonTop = [NSLayoutConstraint constraintWithItem:self.closePageButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:7];
    NSArray *constraints = @[closePageButtonHeight, closePageButtonLeading, closePageButtonTop];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)showFullScreenButtonConstraints {
    NSLayoutConstraint *showFullScreenButtonHeight = [NSLayoutConstraint constraintWithItem:self.showFullScreenButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30];
    NSLayoutConstraint *showFullScreenButtonTop = [NSLayoutConstraint constraintWithItem:self.showFullScreenButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
    NSLayoutConstraint *showFullScreenButtonTrailing = [NSLayoutConstraint constraintWithItem:self.showFullScreenButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10];
    NSArray *constraints = @[showFullScreenButtonTop, showFullScreenButtonTrailing, showFullScreenButtonHeight];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)clearingCurrentDataButtonConstraints {
    NSLayoutConstraint *clearingCurrentDataButtonLeft = [NSLayoutConstraint constraintWithItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.showFullScreenButton attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    NSLayoutConstraint *clearingCurrentDataButtonTop = [NSLayoutConstraint constraintWithItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.showFullScreenButton attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    NSLayoutConstraint *clearingCurrentDataButtonHeight = [NSLayoutConstraint constraintWithItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.showFullScreenButton attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    NSArray *constraints = @[clearingCurrentDataButtonLeft, clearingCurrentDataButtonTop, clearingCurrentDataButtonHeight];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)updateFilterConditionButtonConstraints {
    NSLayoutConstraint *updateFilterConditionButtonTop = [NSLayoutConstraint constraintWithItem:self.updateFilterConditionButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.showFullScreenButton attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *updateFilterConditionButtonWidth = [NSLayoutConstraint constraintWithItem:self.updateFilterConditionButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.showFullScreenButton attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    NSLayoutConstraint *updateFilterConditionButtonHeight = [NSLayoutConstraint constraintWithItem:self.updateFilterConditionButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.showFullScreenButton attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    NSLayoutConstraint *updateFilterConditionButtonTrailing = [NSLayoutConstraint constraintWithItem:self.updateFilterConditionButton attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.showFullScreenButton attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-10];
    NSArray *constraints = @[updateFilterConditionButtonTop, updateFilterConditionButtonWidth, updateFilterConditionButtonHeight, updateFilterConditionButtonTrailing];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)uclearFilterConditionButtonConstraints {
    NSLayoutConstraint *clearFilterConditionButtonTop = [NSLayoutConstraint constraintWithItem:self.clearFilterConditionButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *clearFilterConditionButtonWidth = [NSLayoutConstraint constraintWithItem:self.clearFilterConditionButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    NSLayoutConstraint *clearFilterConditionButtonHeight = [NSLayoutConstraint constraintWithItem:self.clearFilterConditionButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    NSLayoutConstraint *clearFilterConditionButtonTrailing = [NSLayoutConstraint constraintWithItem:self.clearFilterConditionButton attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-10];
    NSArray *constraints = @[clearFilterConditionButtonTop, clearFilterConditionButtonWidth, clearFilterConditionButtonHeight, clearFilterConditionButtonTrailing];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)showFilterConditionViewConstraints {
    NSLayoutConstraint *showFilterConditionViewLeft = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.closePageButton attribute:NSLayoutAttributeRight multiplier:1.0 constant:5];
    NSLayoutConstraint *showFilterConditionViewTop = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *showFilterConditionViewBottom = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.clearFilterConditionButton attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    NSLayoutConstraint *showFilterConditionViewTrailing = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.clearFilterConditionButton attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-6];
    NSArray *constraints = @[showFilterConditionViewLeft, showFilterConditionViewTop, showFilterConditionViewBottom, showFilterConditionViewTrailing];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)floatingListViewLeadingConstraints {
    NSLayoutConstraint *floatingListViewLeading = [WSDataReportHelper equallyRelatedConstraintWithView:self.floatingListView toItem:self.view attribute:NSLayoutAttributeLeading];
    NSLayoutConstraint *floatingListViewTrailing = [WSDataReportHelper equallyRelatedConstraintWithView:self.floatingListView toItem:self.view attribute:NSLayoutAttributeTrailing];
    NSLayoutConstraint *floatingListViewBottom = [WSDataReportHelper equallyRelatedConstraintWithView:self.floatingListView toItem:self.view attribute:NSLayoutAttributeBottom];
    NSLayoutConstraint *floatingListViewTop = [NSLayoutConstraint constraintWithItem:self.floatingListView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.clearingCurrentDataButton attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5];
    NSArray *constraints = @[floatingListViewLeading, floatingListViewTrailing, floatingListViewBottom, floatingListViewTop];
    return constraints;
}


#pragma mark - System Method

/// 自定义UIWindow 需要重写preferredStatusBarStyle和调用setNeedsStatusBarAppearanceUpdate方法,让状态栏显示
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}



@end
